package com.asa.wanandroid_compose.widget

import android.util.Log
import androidx.annotation.DrawableRes
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import  com.asa.wanandroid_compose.R
import com.asa.wanandroid_compose.ui.theme.Color


@Preview
@Composable
fun BottomTabPreView(){
    BottomTab(currentIndex = 0){

    }
}

@Composable
fun BottomTab(currentIndex:Int,currentChange:(Int)->Unit){
    Row(
        Modifier
            .fillMaxWidth()
            .background(Color.bottomBar)
            .padding(4.dp, 4.dp)
    ) {
        TabItem(
            Modifier
                .weight(1f)
                .clickable { currentChange(0) },iconResId = if (currentIndex == 0) R.drawable.ic_tab_home_fill else R.drawable.ic_tab_home, title = "首页",
            tint = if(currentIndex == 0) Color.main else Color.unselect )


        TabItem(
            Modifier
                .weight(1f)
                .clickable { currentChange(1) },iconResId = if (currentIndex == 1) R.drawable.ic_tab_discover_fill else R.drawable.ic_tab_discover, title = "首页",
            tint = if(currentIndex == 1) Color.main else Color.unselect )

        TabItem(
            Modifier
                .weight(1f)
                .clickable { currentChange(2) },iconResId = if (currentIndex == 2) R.drawable.ic_tab_wechat_fill else R.drawable.ic_tab_wechat, title = "首页",
            tint = if(currentIndex == 2) Color.main else Color.unselect )

        TabItem(
            Modifier
                .weight(1f)
                .clickable { currentChange(3) },iconResId = if (currentIndex == 3) R.drawable.ic_tab_my_fill else R.drawable.ic_tab_my, title = "首页",
            tint = if(currentIndex == 3) Color.main else Color.unselect )

    }
}


@Composable
fun TabItem(modifier: Modifier = Modifier,
            @DrawableRes iconResId:Int,
            title:String,
            tint:androidx.compose.ui.graphics.Color
){
    Column(modifier.padding(0.dp, 8.dp, 0.dp, 8.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Icon(painterResource(iconResId), title, Modifier.size(24.dp), tint)
        Text(title, fontSize = 11.sp, color = tint)
    }
}